# get Tkinter
from Tkinter import *
import Tkinter
import tkSimpleDialog

class LEDSettingDraw:
    def change(self):
        self.setting = self.slider.get()

        self.led.redrawSquare(self.naming,self.setting)

        self.led.setName(self.naming)
        self.led.setDutyCycle(self.setting)
        
        #self.percent = (str(round(self.slider.get()*100/255.0,2))), "%"
        #self.percentString.set(self.percent)
 
        self.ntr.delete(0, END)
        self.ntr.insert(0, self.setting)
               
    def done(self):
        self.setting = self.slider.get()
        self.led.setDutyCycle(self.setting)
        
        self.led.func.ledSettingBoxRem()
        
    def cancel(self):
        self.led.redrawSquare(self.oldName,self.oldSetting)

        self.led.setName(self.oldName)
        self.led.setDutyCycle(self.oldSetting)

        self.slider.set(self.oldSetting)
        self.percent = (str(round(self.slider.get()*100/255.0,2))), "%"
        self.percentString.set(self.percent)

        self.led.func.ledSettingBoxRem()

    def enter(self, event):
        text = self.ntr.get()
        if text != "":
            if float(text) > 255:
                self.ntr.delete(0, END)
                self.ntr.insert(0, 255)
            elif float(text) < 0:
                self.ntr.delete(0, END)
                self.ntr.insert(0, 0)
            self.slider.set(self.ntr.get())

    def relabel(self):
        newName = tkSimpleDialog.askstring(title="Relabel", prompt="Enter new name below.")
        while newName == "":
            newName = tkSimpleDialog.askstring(title="Relabel", prompt="Invalid name. Please reenter new name.")
        self.led.redrawSquare(newName, self.setting)

        
    def __init__(self, master, led):
        self.led = led
        self.naming = led.name.get()
        self.setting = led.dutySetting.get()
        self.master = Frame(master)
        self.oldName = self.naming
        self.oldSetting = self.setting
        
        # This panel will be a 5 row by 3 column grid.
        self.nameChanger = StringVar()
        self.nameChanger.set(self.naming)
        self.nameLabel = Message(self.master, width = 50, textvariable=self.nameChanger)
        self.nameLabel.grid(row=0, column=0, sticky=W) # LED display
        self.relabelButton = Button(self.master, text="Relabel", command = self.relabel)
        self.relabelButton.grid(row=1, column=0, sticky=W) # Relabel button
        
        # Main "Duty Cycle" label for this panel
        self.dutyCycleLabel = Label(self.master, text="Duty Cycle")
        self.dutyCycleLabel.grid(row=1, column=1, sticky=W+E) 
        
        #self.percent=IntVar()
        # create the scrollbar
        self.slider = Scale(self.master, from_=0, to=255, length=255, orient=HORIZONTAL, command = self.redrawPercentage)
        self.slider.set(self.setting)
        self.slider.grid(row=2, column=0, columnspan=3, sticky=W+E) # placing the scrollbar
        
        self.ntr = Entry(self.master, width=8) # Creating and placing the entry box
        self.ntr.insert(0, self.setting)
        self.ntr.grid(row=3, column=0)
        self.ntr.bind("<Return>", self.enter)
        
        
        
        # buttons to adjust the LED
        self.doneButton = Button(self.master, text="Done", command = self.done)
        self.changeButton = Button(self.master, text="Change", command = self.change)
        self.cancelButton = Button(self.master, text="Cancel", command = self.cancel)

        self.doneButton.grid(row=4, column=0, sticky=W, pady=10)
        self.changeButton.grid(row=4, column=1, pady=10)
        self.cancelButton.grid(row=4, column=2, sticky=E, pady=10)

    def redrawPercentage(self, event):
        self.percent = round(self.slider.get()*100/255.0,2)
        
        self.percentString = StringVar()
        self.percentString.set(str(self.percent) + ' %')
        self.percentLabel = Label(self.master, textvariable=self.percentString)
        self.percentLabel.grid(row=3, column=2)
        
    def retSettingFrame(self):
        return self.master

    def destroy(self):
        self.master.destroy()
